﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace N進数電卓
{
    

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        
        private Button[] bm = new Button[36];
        
        private void convertbutton_Click(object sender, EventArgs e)
        {

            //N進数を10進に変換
            string ctr = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            string str = fromtextbox.Text;
            int nn = Convert.ToInt16(fromnumber.Value);
            int ii = 0;
            int jj = 0;
            int kk = 1;
            int ll;
            int number = 0;
            ii = (fromtextbox.Text.Length) - 1;

            for (jj = 0; jj < ii; jj++)
            {
                kk *= nn;
            }

            for (jj = 0; jj <= ii; jj++, kk /= nn)
            {
                for (ll = 0; ll < nn; ll++)
                {
                    if (str.Substring(jj, 1).Equals(ctr.Substring(ll, 1)))
                    {
                        number += ll * kk;
                    }
                }
            }

            //10進数をN進数に変換

            nn = Convert.ToInt16(tonumber.Value);
            kk = 0;
            int[] ssr = new int[256];

            for (ii = 0; number / nn > 0; ii++, kk++)
            {
                ssr[ii] = number % nn;
                number /= nn;
            }

            totextbox.Text = Convert.ToString(number);

            for (ii = kk; ii > 0; ii--)
            {
                totextbox.Text += ctr.Substring(ssr[ii - 1], 1);
            }
        }

        private void fromnumber_ValueChanged(object sender, EventArgs e)
        {

            //ボタンを配列化する
            bm[0] = zero;
            bm[1] = one;
            bm[2] = two;
            bm[3] = three;
            bm[4] = four;
            bm[5] = five;
            bm[6] = six;
            bm[7] = seven;
            bm[8] = eight;
            bm[9] = nine;
            bm[10] = a;
            bm[11] = b;
            bm[12] = c;
            bm[13] = d;
            bm[14] = ee;
            bm[15] = f;
            bm[16] = g;
            bm[17] = h;
            bm[18] = i;
            bm[19] = j;
            bm[20] = k;
            bm[21] = l;
            bm[22] = m;
            bm[23] = n;
            bm[24] = o;
            bm[25] = p;
            bm[26] = q;
            bm[27] = r;
            bm[28] = s;
            bm[29] = t;
            bm[30] = u;
            bm[31] = v;
            bm[32] = w;
            bm[33] = x;
            bm[34] = y;
            bm[35] = z;

            for (int ij = 0; ij < 36; ij++)
            {
                int xx = Convert.ToInt16(fromnumber.Value);
                if (ij < xx)
                {
                    bm[ij].Enabled = true;
                }
                else
                {
                    bm[ij].Enabled = false;
                }

            }

            fromtextbox.Clear();
        }

        private void tonumber_ValueChanged(object sender, EventArgs e)
        {
            totextbox.Clear();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            //ボタンを配列化する
            bm[0] = zero;
            bm[1] = one;
            bm[2] = two;
            bm[3] = three;
            bm[4] = four;
            bm[5] = five;
            bm[6] = six;
            bm[7] = seven;
            bm[8] = eight;
            bm[9] = nine;
            bm[10] = a;
            bm[11] = b;
            bm[12] = c;
            bm[13] = d;
            bm[14] = ee;
            bm[15] = f;
            bm[16] = g;
            bm[17] = h;
            bm[18] = i;
            bm[19] = j;
            bm[20] = k;
            bm[21] = l;
            bm[22] = m;
            bm[23] = n;
            bm[24] = o;
            bm[25] = p;
            bm[26] = q;
            bm[27] = r;
            bm[28] = s;
            bm[29] = t;
            bm[30] = u;
            bm[31] = v;
            bm[32] = w;
            bm[33] = x;
            bm[34] = y;
            bm[35] = z;

            for (int ij = 0; ij < 36; ij++)
            {
                int xx = Convert.ToInt16(fromnumber.Value);
                if (ij < xx)
                {
                    bm[ij].Enabled = true;
                }
                else
                {
                    bm[ij].Enabled = false;
                }

            }

        }
    }
}
